package com.khstudio.api.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.khstudio.api.util.KhLog;

public class KhImageBoxView extends View {
    private Drawable    mDrawableBase;
    private Drawable    mDrawableBox;

    private Bitmap      mBm;
    private Rect        mRect;

    private Paint mPaint = new Paint();

    private int mWidthBox;
    private int mHeightBox;

    public KhImageBoxView(Context context) {
        this(context, null);
    }

    public KhImageBoxView(Context context,AttributeSet attrs) {
        this(context,attrs,0);
    }

    public KhImageBoxView(Context context,AttributeSet attrs,int defStyle) {
        super(context,attrs,defStyle);

        mDrawableBase = null;
        mDrawableBox = null;

        mBm = null;
        mRect = new Rect();

        mWidthBox = 0;
        mHeightBox = 0;
    }

    public void setBaseDrawable(Drawable d) {
        mDrawableBase = d;
        mWidthBox  = mDrawableBase.getIntrinsicWidth();
        mHeightBox = mDrawableBase.getIntrinsicHeight();

        if(mDrawableBox != null) {
            int width = mDrawableBase.getIntrinsicWidth();
            int height = mDrawableBase.getIntrinsicHeight();
            mWidthBox = mWidthBox > width ? mWidthBox : width;
            mHeightBox = mHeightBox > height ? mHeightBox : height;
        } else {
            mWidthBox = mDrawableBase.getIntrinsicWidth();
            mHeightBox = mDrawableBase.getIntrinsicHeight();
        }
        KhLog.e("setBaseDrawable w = %d h = %d", mWidthBox, mHeightBox);
    }

    public void setBoxDrawable(Drawable d) {
        mDrawableBox = d;
        mWidthBox  = mDrawableBox.getIntrinsicWidth();
        mHeightBox = mDrawableBox.getIntrinsicHeight();

        if(mDrawableBase != null) {
            int width = mDrawableBox.getIntrinsicWidth();
            int height = mDrawableBox.getIntrinsicHeight();
            mWidthBox = mWidthBox > width ? mWidthBox : width;
            mHeightBox = mHeightBox > height ? mHeightBox : height;
        } else {
            mWidthBox = mDrawableBox.getIntrinsicWidth();
            mHeightBox = mDrawableBox.getIntrinsicHeight();
        }
        KhLog.e("setBoxDrawable w = %d h = %d", mWidthBox, mHeightBox);
    }

    public void setImage(Bitmap b) {
        if(mBm != null) {
            mBm.recycle();
            mBm = null;
        }

        mBm = b;
        invalidate();
    }

    public int geWidthBox()   { return mWidthBox;  }
    public int getHeightBox() { return mHeightBox; }

    public Rect getRectImage() {
        int l = getLeft() + mRect.left;
        int r = l + mRect.right - mRect.left;
        int t = getTop() + mRect.top;
        int b = t + mRect.bottom - mRect.top;
        return new Rect(l, t, r, b);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if(changed) {
            int width = right - left;
            int height = bottom - top;

            mDrawableBase.setBounds(0, 0, width, height);
            mDrawableBox.setBounds(0, 0, width, height);

            int l = width / 6 / 2;
            int r = l + width - width / 6;
            int t = height / 6 / 2;
            int b = t + height - height / 6;
            mRect.set(l, t, r, b);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(mBm == null) {
            mDrawableBase.draw(canvas);
        } else {
            canvas.save();
            canvas.translate(0 , 0);
            canvas.drawBitmap(mBm, null, mRect, mPaint);
            mDrawableBox.draw(canvas);
            canvas.restore();
        }
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        int[] state = getDrawableState();

        if (mDrawableBase != null && mDrawableBase.isStateful()) {
            mDrawableBase.setState(state);
        }

        if (mDrawableBox != null && mDrawableBox.isStateful()) {
            mDrawableBox.setState(state);
        }

        invalidate();
    }

    @Override
    protected boolean verifyDrawable(Drawable who) {
        return super.verifyDrawable(who) || who == mDrawableBase || who == mDrawableBox;
    }
}
